﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.DAL;
using Gift.Model;
using Gift.Model.ENUM;
using System.Text;
using System.Data;

namespace WebSite.admin.warehouse
{
    public partial class stockGoodsOut : BaseAdmin
    {
        tbGoods oGoods;
        tbWarehouseGoodsOut oWarehouseGoodsOut;
        readonly DALWarehouse theWarehouse = new DALWarehouse();
        readonly DALWarehouseGoods theWarehouseGoods = new DALWarehouseGoods();
        readonly DALWarehouseGoodsOut theWarehouseGoodsOut = new DALWarehouseGoodsOut();
        readonly DALGoods theGoods = new DALGoods();
        readonly DALModelType theModelType = new DALModelType();
        readonly DALStockOrder theStockOrder = new DALStockOrder();
        tbModelType oModelType;
        int iGoodsOutCount = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.StockOutOrderList);
                this.BuildForm();
                this.BindWarehouse();
               // this.BindGoodsBatchNoList();

            }
        }

        //void BindGoodsBatchNoList()
        //{
        //    if (this.ddlWarehouse.SelectedItem.Value != "-1")
        //    {
        //        string sWhere = string.Format(" goodsId = {0} and warehouseId = {1} "
        //                                       , Request.QueryString["gid"]
        //                                       , this.ddlWarehouse.SelectedItem.Value);
        //        DataTable tbBatch = theWarehouseGoods.GetList(sWhere).Tables[0];
        //        this.ddlGoodsBatchNo.DataTextField = "goodsBatchNo";
        //        this.ddlGoodsBatchNo.DataValueField = "goodsBatchNo";
        //        this.ddlGoodsBatchNo.DataSource = tbBatch;
        //        this.ddlGoodsBatchNo.DataBind();
        //    }

        //}

        void BuildForm()
        {
            oGoods = theGoods.GetModel(int.Parse(Request.QueryString["gid"]));
            this.lblGoodsName.Text = oGoods.goodsName;
            this.lblGoodsNo.Text = oGoods.goodsCode;
            if (!string.IsNullOrEmpty(Request.QueryString["mid"]))
            {
                oModelType = theModelType.GetModel(int.Parse(Request.QueryString["mid"]));
                if (oModelType != null)
                {
                    this.lblModelType.Text = "商品型号：" + oModelType.modelTypeName;
                }
            }
            DataTable dt = theWarehouseGoodsOut.GetList(string.Format(" orderId={0} and goodsId={1} and modelTypeId={2}"
                                                                        , Request.QueryString["oid"]
                                                                        , Request.QueryString["gid"]
                                                                        , string.IsNullOrEmpty(Request.QueryString["mid"]) ? "0" : Request.QueryString["mid"]
                                                                        )).Tables[0];

            StringBuilder sbDescription = new StringBuilder();
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    sbDescription.AppendFormat("库房：{0},出库数量：{1},"
                                                , theWarehouse.GetModel(int.Parse(dr["warehouseId"].ToString())).warehouseName
                                                , dr["goodsCount"].ToString()
                                                );
                    iGoodsOutCount = iGoodsOutCount + int.Parse(dr["goodsCount"].ToString());
                }
                ViewState["goodsOutCount"] = iGoodsOutCount;
            }
            int iOutCount = 0;
            if (ViewState["goodsOutCount"] != null)
            {
                iOutCount = int.Parse(Request.QueryString["gcount"]) - int.Parse(ViewState["goodsOutCount"].ToString());
            }
            else
            {
                iOutCount = int.Parse(Request.QueryString["gcount"]);
            }
            sbDescription.AppendFormat("您还需要出库商品{0}件。", iOutCount);
            this.lblOutDescription.Text = sbDescription.ToString();
            this.txtGoodsOutCount.Text = iOutCount.ToString();
        }

        void BindWarehouse()
        {
            int iModelTypeId = 0;
            if (!string.IsNullOrEmpty(Request.QueryString["mid"]))
            {
                iModelTypeId = int.Parse(Request.QueryString["mid"]);
            }
            DataTable dt = theWarehouse.GetWarehouseGoodsList(int.Parse(Request.QueryString["gid"]), iModelTypeId).Tables[0];
            if (dt != null)
            {
                this.ddlWarehouse.DataTextField = "warehouseGoodsName";
                this.ddlWarehouse.DataValueField = "id";
                this.ddlWarehouse.DataSource = dt;
                this.ddlWarehouse.DataBind();
            }
            this.ddlWarehouse.Items.Insert(0, new ExtAspNet.ListItem("请选择仓库", "-1"));
            this.ddlWarehouse.SelectedValue = "-1";

        }


        protected void btnSubmitForm1_Click(object sender, EventArgs e)
        {
            int iOutCount = 0;
            if (ViewState["goodsOutCount"] != null)
            {
                iOutCount = int.Parse(Request.QueryString["gcount"]) - int.Parse(ViewState["goodsOutCount"].ToString());
            }
            else
            {
                iOutCount = int.Parse(Request.QueryString["gcount"]);
            }
            if (iOutCount >= int.Parse(this.txtGoodsOutCount.Text))
            {
                oWarehouseGoodsOut = new tbWarehouseGoodsOut();

                oWarehouseGoodsOut.creater = int.Parse(base.AdminUserId);
                oWarehouseGoodsOut.createTime = DateTime.Now;
                oWarehouseGoodsOut.goodsCount = int.Parse(this.txtGoodsOutCount.Text);
                oWarehouseGoodsOut.goodsId = int.Parse(Request.QueryString["gid"]);
                oWarehouseGoodsOut.orderId = long.Parse(Request.QueryString["oid"]);
                oWarehouseGoodsOut.warehouseId = int.Parse(this.ddlWarehouse.SelectedItem.Value);
             //   oWarehouseGoodsOut.GoodsBatchNo = this.ddlGoodsBatchNo.SelectedItem.Value;
                oWarehouseGoodsOut.remark = this.txtRemark.Text.Trim();
                oWarehouseGoodsOut.OrderType = 1;
                if (!string.IsNullOrEmpty(Request.QueryString["gid"]))
                {
                    oWarehouseGoodsOut.ModelTypeId = int.Parse(Request.QueryString["mid"]);
                }

                if (theWarehouseGoodsOut.Add(oWarehouseGoodsOut, Request.QueryString["ocode"]) > 0)
                {
                    theStockOrder.UpdateStockOutOrderStates(Request.QueryString["ocode"], int.Parse(Request.QueryString["oid"]), 1);
                    ExtAspNet.Alert.Show("出库成功！");
                    ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference());

                }
                else
                {
                    ExtAspNet.Alert.Show("出库失败！");
                    //base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"出库失败！\");</script>");
                }
            }
            else
            {
                ExtAspNet.Alert.Show("商品出库数量不能大于订单商品数量！");
                //base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"商品出库数量不能大于订单商品数量！\");</script>");
            }

        }

    }
}